package edu.mtu.citizenscience.streamapplication.activity.main;

import edu.mtu.citizenscience.streamapplication.activity.DBHelper;
import edu.mtu.citizenscience.streamapplication.activity.R;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.view.Menu;
import android.view.View;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.RadioGroup;
import android.widget.RadioButton;
import android.widget.ScrollView;
import android.widget.Spinner;

public class SCCulvertActivity extends Activity {

	public static final String ROW_ID = "ROW_ID";
	private Long rowID;
	private DBHelper myDbHelper;
	
	private Cursor report;
	
	private EditText lengthText;
	private EditText diameterText;
	private Spinner materialSpin;
	private RadioGroup conditionRadios;
	private RadioButton conditionGood;
	private RadioButton conditionFair;
	private RadioButton conditionPoor;
	private CheckBox fishCheckbox;
	private RadioGroup flowRadios;
	private RadioButton flowClear;
	private RadioButton flowObstructed;
	private RadioButton flowPerched;
	private EditText inletFillText;
	private EditText outletFillText;
	private Spinner inletEmbSpin;
	private Spinner outletEmbSpin;
	
	private RadioButton selectedRadioC;
	private RadioButton selectedRadioF;
	private String checkboxString, conditionString, flowString;

	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_scculvert);
		
		myDbHelper = new DBHelper(this);
		myDbHelper.open();
		
		lengthText = (EditText) findViewById(R.id.culvLength);
		diameterText = (EditText) findViewById(R.id.culvDiameter);
		materialSpin = (Spinner) findViewById(R.id.culvMaterial);
		conditionRadios = (RadioGroup) findViewById(R.id.conditionRGroup);
		conditionGood = (RadioButton) findViewById(R.id.radio_conditiongood);
		conditionFair = (RadioButton) findViewById(R.id.radio_conditionfair);
		conditionPoor = (RadioButton) findViewById(R.id.radio_conditionpoor);
		fishCheckbox = (CheckBox) findViewById(R.id.checkboxFishPassage);
		flowRadios = (RadioGroup) findViewById(R.id.flowRGroup);
		flowClear = (RadioButton) findViewById(R.id.radio_flowclear);
		flowObstructed = (RadioButton) findViewById(R.id.radio_flowobstructed);
		flowPerched = (RadioButton) findViewById(R.id.radio_flowperched);
		inletFillText = (EditText) findViewById(R.id.inletFillText);
		outletFillText = (EditText) findViewById(R.id.outletFillText);
		inletEmbSpin = (Spinner) findViewById(R.id.embInletSpinner);
		outletEmbSpin = (Spinner) findViewById(R.id.embOutletSpinner);
		

		
				
		
		rowID = this.getIntent().getLongExtra(this.ROW_ID, -1);
		populateFields();
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.scculvert, menu);
		return true;
	}
	
	
	private void populateFields() {
    	report = myDbHelper.fetchReport(rowID, "Culvert_Desc");
    	
        if (report.getCount() == 1) {
            lengthText.setText(""+report.getInt(report.getColumnIndexOrThrow("length")));
            diameterText.setText(""+report.getInt(report.getColumnIndexOrThrow("diameter")));
           
            //compare text to options, materialSpin.setSelection(index) to the position of the matching option
            materialSpin.setSelection(getSpinnerIndex(materialSpin, report.getString(report.getColumnIndexOrThrow("material"))));
            
            //compare conditionRadios to string in database and select matching
            conditionString = report.getString(report.getColumnIndexOrThrow("condition")); 
            if(conditionString.equals("Good"))
            	conditionRadios.check(R.id.radio_conditiongood);
            else if(conditionString.equals("Fair"))
            	conditionRadios.check(R.id.radio_conditionfair);
            else if(conditionString.equals("Poor"))
            	conditionRadios.check(R.id.radio_conditionpoor);
            else
            	conditionRadios.check(-1);
            	
            if(report.getString(report.getColumnIndexOrThrow("fish_passage_problem")).equals("true"))
            	fishCheckbox.setChecked(true);
            else
            	fishCheckbox.setChecked(false);
            
            //compare flowRadios to string in database and select matching
            flowString = report.getString(report.getColumnIndexOrThrow("flow"));     
            if(flowString.equals("Clear"))
            	flowRadios.check(R.id.radio_flowclear);
            else if(flowString.equals("Obstructed"))
            	flowRadios.check(R.id.radio_flowobstructed);
            else if(flowString.equals("Perched at culvert"))
            	flowRadios.check(R.id.radio_flowperched);
            else
            	flowRadios.check(-1);
            
            inletFillText.setText(""+report.getInt(report.getColumnIndexOrThrow("fill_depth_inlet")));
            outletFillText.setText(""+report.getInt(report.getColumnIndexOrThrow("fill_depth_outlet")));
            inletEmbSpin.setSelection(getSpinnerIndex(inletEmbSpin, report.getString(report.getColumnIndexOrThrow("embankment_inlet"))));
            outletEmbSpin.setSelection(getSpinnerIndex(outletEmbSpin, report.getString(report.getColumnIndexOrThrow("embankment_outlet"))));
          
        }
        else
        {
        	conditionRadios.check(-1);
        	flowRadios.check(-1);
        }
    }
    
    private void saveLocation(){
    	String selectedCString, selectedFString;
    	
    	if(fishCheckbox.isChecked())
			checkboxString = "true";
		else
			checkboxString = "false";

		if((lengthText.getText().toString()).length()==0)
			lengthText.setText("0");
		if((diameterText.getText().toString()).length()==0)
			diameterText.setText("0");
		if((inletFillText.getText().toString()).length()==0)
			inletFillText.setText("0");
		if((outletFillText.getText().toString()).length()==0)
			outletFillText.setText("0");
		
		selectedRadioC = (RadioButton) findViewById(conditionRadios.getCheckedRadioButtonId());
		selectedRadioF = (RadioButton) findViewById(flowRadios.getCheckedRadioButtonId());
		
		if(conditionRadios.getCheckedRadioButtonId() > 0)
			selectedCString=selectedRadioC.getText().toString();
		else
			selectedCString="Empty";
		
		if(flowRadios.getCheckedRadioButtonId() > 0)
			selectedFString=selectedRadioF.getText().toString();
		else
			selectedFString="Empty";
		
		if(report.getCount() == 0){
			
			myDbHelper.createCulvert(rowID, Integer.parseInt(lengthText.getText().toString()), Integer.parseInt(diameterText.getText().toString()),
					materialSpin.getSelectedItem().toString(), selectedCString, selectedFString, checkboxString,
					Integer.parseInt(inletFillText.getText().toString()), Integer.parseInt(outletFillText.getText().toString()), inletEmbSpin.getSelectedItem().toString(),
					outletEmbSpin.getSelectedItem().toString());
		}
		else{
			myDbHelper.updateCulvert(rowID, Integer.parseInt(lengthText.getText().toString()), Integer.parseInt(diameterText.getText().toString()),
					materialSpin.getSelectedItem().toString(), selectedCString, selectedFString, checkboxString,
					Integer.parseInt(inletFillText.getText().toString()), Integer.parseInt(outletFillText.getText().toString()), inletEmbSpin.getSelectedItem().toString(),
					outletEmbSpin.getSelectedItem().toString());
		}
    }
    
    private int getSpinnerIndex(Spinner spin, String str)
    {
    	for(int i=0; i < spin.getCount(); i++)
    	{
    		if(spin.getItemAtPosition(i).equals(str))
    			return i;
    	}
    	return -1;
    }
    
	/** Called when the user clicks the Continue button */
	public void cont(View view) {
		saveLocation();
		myDbHelper.close();
	    
		this.finish();
	}
	
	public void cancel(View view) {
		myDbHelper.close();
	    
		this.finish();
	}

}
